import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from common.Utils import TestDriver


# 测试问卷考试系统注册页面
class RegisterTest:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = TestDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

    def RegisterSuccess(self):
        #注册测试实例1
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("zhangzhang")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangzhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        time.sleep(2)
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(3)

    # 注册失败测试用例
    def RegisterFail(self):
        # 注册重复账号测试用例
        # 输入注册信息
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test78")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test78")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        time.sleep(1)
        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()

        TestDriver.getScreenshot()

        assert self.driver.find_element(By.CSS_SELECTOR,
                                        "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text == "账号已存在"

        print(1)
        # 注册不输入用户名、但输入登陆账号、密码、确认密码后进行注册
        # 清空输入框
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        time.sleep(1)
        # 输入注册信息
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test789")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        time.sleep(1)
        TestDriver.getScreenshot()
        assert self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text == "用户名是必填项！"

        # 密码和确认密码不一直
        # 清空输入框
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))
        # 输入注册信息
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test789")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test789")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        time.sleep(1)
        TestDriver.getScreenshot()
        assert self.driver.find_element(By.CSS_SELECTOR, "#rePassword_help > div").text == "两次密码不一致!"